﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Portfolio_Tracker
{
    public partial class ChartRequestForm : Form
    {
        public ChartRequestForm(string tickerSymbol)
        {
            InitializeComponent();

            tickerTextBox.Text = tickerSymbol;

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            string timeSpanUnit = "", scale ="";

            
            switch (timespanComboBox.Text)
            {
                case "Days":
                    timeSpanUnit = "d";
                    break;
                case "Months":
                    timeSpanUnit = "m";
                    break;
                case "Years":
                    timeSpanUnit = "y";
                    break;
                case "":
                    MessageBox.Show("Invalid timespan unit");
                    break;
            }

            switch (typeComboBox.Text)
            {
                case "Line":
                    scale = "l";
                    break;
                case "Candle":
                    scale = "c";
                    break;
                case "Bar":
                    scale = "b";
                    break;
                case "":
                    MessageBox.Show("Invalid chart type");
                    break;
            }

            try
            {
                string beginDownloadURL = "http://chart.finance.yahoo.com/z?s="; // EX: http://chart.finance.yahoo.com/z?s=FTT.TO&t=1d&q=l
                string middleDownloadURL = tickerTextBox.Text;
                string endURL = "&t=" + timespanTextBox.Text + timeSpanUnit + "&q=" + scale;

                Process.Start(beginDownloadURL + middleDownloadURL + endURL);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            this.Close();
        }

    }
}
